<?php

namespace yak\layui\controllers;

use Yii;
use yak\layui\models\admin\User;
use yak\layui\models\admin\User as Admin;
use yak\layui\models\rbac\AssignForm;
use yak\layui\search\AdminSearch;
use yii\web\NotFoundHttpException;

/**
 * AdminController implements the CRUD actions for Admin model.
 */
class AdminController extends BaseController
{
    /**
     * Lists all Admin models.
     * @return mixed
     */
    public function actionIndex()
    {
        $searchModel = new AdminSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single Admin model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->renderPartial('_view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * @return string
     * @throws \yii\base\Exception
     * Creates a new Admin model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     */
    public function actionCreate()
    {
        $this->layout = 'blank';
        $model = new Admin();
        $model->setPassword(123456);
        $model->generateAuthKey();

        return $this->render('_form', [
            'model' => $model,
            'ajaxOptions' => [
                'modelClass' => $this->ajaxHeaderClass($model),
            ]
        ]);
    }

    /**
     * Updates an existing Admin model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $this->layout = 'blank';
        $model = $this->findModel($id);

        return $this->render('_form', [
            'model' => $model,
            'ajaxOptions' => [
                'modelClass' => $this->ajaxHeaderClass($model),
                'condition' => ['id' => $id]
            ]
        ]);
    }

    /**
     * Deletes an existing Admin model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();

        return $this->ajaxSuccess();
    }

    /**
     * Deletes an existing Admin model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionBatchDelete($id)
    {
        $ids = Yii::$app->request->post('ids', []);
        Admin::deleteAll(['id' => $ids]);

        return $this->ajaxSuccess();
    }

    /**
     * Output Excel.
     */
    public function actionOutput()
    {
    }

    public function actionResetPassword($password)
    {
        /**
         * @var $user User
         */
        $user = Yii::$app->user->identity;
        $user->setPassword($password);
        $user->removePasswordResetToken();
        return $user->save(false) ?
            $this->ajaxSuccess() : $this->ajaxError();
    }

    public function actionAssign($id, $name)
    {
        if (Yii::$app->request->isPost) {
            $model = new AssignForm();
            if ($model->load(Yii::$app->request->post(), '') && $model->setChildrenToUser($id)) {
                Yii::$app->session->setFlash('success', '分配用户权限成功');
                return $this->redirect(['index']);
            }
        }

        return $this->render('assign', [
            'permissions' => AssignForm::permissionsLayuiTreeList(),
            'roles' => AssignForm::rolesTreeList(),
            'userId' => $id, 'name' => $name
        ]);
    }

    /**
     * Finds the Admin model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Admin the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Admin::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
